/**ai出牌 */
var PkCardTool = require('../../FindTool/PkCardTool');
var _ = require('../../../FrameWork/we_underscore');
var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var EumPkGroup = require('../../Enum/EumPkGroup');
var AIGetOne = module.exports = {};

AIGetOne.doMore = function(_lastCardInfo, _cardsInfo) {
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 1) {
        return {
            isFind: false
        };
    }

    var result = {
        allGetCardsGroup: []
    };
    for (var i = 1; i <= 4; ++i) {
        var tmpResult = this.onlySingleGroup(_lastCardInfo, _cardsInfo, i);
        if (tmpResult.isFind) {
            result.allGetCardsGroup.push(tmpResult.allGetCardsGroup);
        }
    }
    if (_.size(result.allGetCardsGroup) > 0) {
        result.isFind = true;
        result.allGetCardsGroup = _.flatten(result.allGetCardsGroup);
        return result;
    }
    return {
        isFind: false
    };
};

/***AI出牌
 * _cardsInfo:[1,2,3,6];
 */
AIGetOne.do = function(_lastCardInfo, _cardsInfo) {
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 1) {
        return {
            isFind: false
        };
    }

    /**只从单牌里面找 */
    for (var i = 1; i < 4; ++i) {
        var result = this.onlySingleGroup(_lastCardInfo, _cardsInfo, i);
        if (result.isFind) {
            return result;
        }
    }
    return {
        isFind: false
    };
};

/***找单牌
 * _cardsInfo:[{}];
 */
AIGetOne.onlySingleGroup = function(_lastCardInfo, _cardsInfo, _sameCardCnt) {
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 1) {
        return {
            isFind: false
        };
    }
    /**需要要起得牌 */
    var needGetCardInfo = _lastCardInfo.cardsInfo[0];

    var needGetValue = needGetCardInfo.value;
    var canGetCardsInfo = _.filter(_cardsInfo, function(_cardInfo) {
        return _cardInfo.value > needGetValue
    });
    // canGetCardsInfo = _.sortBy(canGetCardsInfo, function(_cardInfo) {
    //     return _cardInfo.value
    // });
    var sameCardsList = FindSameCardValueTool.check(canGetCardsInfo);

    var singleList = _.filter(sameCardsList, function(cardList) {
        return _sameCardCnt == _.size(cardList);
    });

    if (_.size(singleList) < 1) {
        return {
            isFind: false
        };
    }
    singleList = _.map(singleList, function(cardsInfo) {
        return cardsInfo[0];
    });
    if (_.size(singleList) > 0) {
        var choiceCardsInfo = singleList[0];
        return {
            isFind: true,
            type: EumPkGroup.ONE,
            recommendCards: [choiceCardsInfo], //推荐组
            allGetCardsGroup: singleList //所有组 
        };
    }
    return {
        isFind: false
    };
};